package com.idemon.config;

import org.apache.log4j.Logger;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.idemon.commons.util.CodecKit;
import com.idemon.ds.model._MappingKit;
import com.idemon.ds.util.SessionInterceptor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

import freemarker.template.TemplateModelException;

public class GlobalConfig extends JFinalConfig {
	private Logger log = Logger.getLogger(getClass());

	@Override
	public void configRoute(Routes me) {
		me.add(new FrontRoutes());
	}

	@Override
	public void configConstant(Constants me) {
		PropKit.use(Consts.FILENAME_CONFIG);
		me.setDevMode(PropKit.getBoolean(Consts.KEY_DEVMODE, true));
		me.setViewType(ViewType.FREE_MARKER);
		me.setFreeMarkerTemplateUpdateDelay(PropKit.getInt(Consts.KEY_FREEMARKER_DELAY, 3600));
		me.setEncoding(PropKit.get(Consts.KEY_ENCODING, "UTF-8"));
		me.setBaseUploadPath(PropKit.get(Consts.KEY_FILE_UPLOAD_PATH, "images"));
	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置druid数据库连接池插件
		String url = CodecKit.decryptByDES(PropKit.get(Consts.KEY_JDBCURL), PropKit.get(Consts.KEY_PROJECT_NAME),
				PropKit.get(Consts.KEY_ENCODING));
		String user = CodecKit.decryptByDES(PropKit.get(Consts.KEY_USER), PropKit.get(Consts.KEY_PROJECT_NAME),
				PropKit.get(Consts.KEY_ENCODING));
		String password = CodecKit.decryptByDES(PropKit.get(Consts.KEY_PASSWORD), PropKit.get(Consts.KEY_PROJECT_NAME),
				PropKit.get(Consts.KEY_ENCODING));
		DruidPlugin druidPlugin = new DruidPlugin(url, user, password);
		druidPlugin.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType(Consts.KEY_MYSQL);
		druidPlugin.addFilter(wall);
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setDialect(new MysqlDialect());
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp);
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configHandler(Handlers me) {
		FreeMarkerRender.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss");
		FreeMarkerRender.setProperty("date_format", "yyyy-MM-dd");
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInterceptor());
	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		try {
			FreeMarkerRender.getConfiguration().setSharedVariable(Consts.ATTR_KEY_CONTEXT_PATH,
					PropKit.get(Consts.ATTR_KEY_CONTEXT_PATH, "/"));
		} catch (TemplateModelException e) {
			log.error("初始化ContextPath失败", e);
		}
	}
}
